﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection.Emit;
using System.Text;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.Ex.Communication.Net.DataTransfer.Base
{
    /// <summary>
    /// 数据传输接口
    /// </summary>
    public interface IDataTransfer : IDisposableZ
    {
        /// <summary>
        /// 接收到数据输出事件
        /// </summary>
        event EventHandler<DataTransferDataOutputArgs> ReceiveData;

        /// <summary>
        /// 异步发送完成事件
        /// </summary>
        event EventHandler<AsynSendCompletedArgs> AsyncSendCompleted;



        /// <summary>
        /// Tag
        /// </summary>
        object Tag { get; set; }

        /// <summary>
        /// 开始收发数据
        /// </summary>
        void Start();

        /// <summary>
        /// 让步收发数据
        /// </summary>
        void Stop();





        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buffer">发送的数据</param>
        /// <param name="options">发送选项</param>
        void Send(byte[] buffer, ITransferDataOptions options);

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buffer">发送的数据</param>
        /// <param name="startIndex">发送数据起始位置</param>
        /// <param name="count">发送数据长度</param>
        /// <param name="options">发送选项</param>
        void Send(byte[] buffer, int startIndex, int count, ITransferDataOptions options);

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="bufferData">发送的数据</param>
        /// <param name="options">发送选项</param>
        void Send(BufferData bufferData, ITransferDataOptions options);

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data">要发送的数据对象</param>
        /// <param name="options">发送选项</param>
        void Send(TransferDataAbs data, ITransferDataOptions options);





        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="buffer">发送的数据</param>
        /// <param name="options">发送选项</param>
        /// <returns>返回发的送资源标识</returns>
        int SendAsync(byte[] buffer, ITransferDataOptions options);

        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="buffer">发送的数据</param>
        /// <param name="startIndex">发送数据起始位置</param>
        /// <param name="count">发送数据长度</param>
        /// <param name="options">发送选项</param>
        /// <returns>返回发的送资源标识</returns>
        int SendAsync(byte[] buffer, int startIndex, int count, ITransferDataOptions options);

        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="bufferData">发送的数据</param>
        /// <param name="options">发送选项</param>
        /// <returns>返回发的送资源标识</returns>
        int SendAsync(BufferData bufferData, ITransferDataOptions options);

        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="data">要发送的数据对象</param>
        /// <param name="options">发送选项</param>
        /// <returns>返回发的送资源标识</returns>
        int SendAsync(TransferDataAbs data, ITransferDataOptions options);



        /// <summary>
        /// 取消发送
        /// </summary>
        /// <param name="rid">资源标识</param>
        void CancellSend(int rid);

    }

    /// <summary>
    /// 数据传输结果
    /// </summary>
    public enum DataTransferResult
    {
        /// <summary>
        /// 成功
        /// </summary>
        Succes,

        /// <summary>
        /// 失败(网络不通/对方不在线/操作超时/发送队列满后溢出等)
        /// </summary>
        Fail,

        /// <summary>
        /// 对方拒绝
        /// </summary>
        Refuse,

        /// <summary>
        /// 己方取消
        /// </summary>
        Cancell
    }

    /// <summary>
    /// 异步发送完成事件参数
    /// </summary>
    public class AsynSendCompletedArgs : EventArgs
    {
        /// <summary>
        /// 发送资源标识
        /// </summary>
        public int RID { get; private set; }

        /// <summary>
        /// 指示异步发送结果成功或失败[true:成功;false:失败]
        /// </summary>
        public DataTransferResult AsynSendResult { get; private set; }

        /// <summary>
        /// 发生的异常信息
        /// </summary>
        public Exception Exception { get; private set; }


        /// <summary>
        /// 构造函数
        /// </summary>
        public AsynSendCompletedArgs()
        {

        }

        /// <summary>
        /// 更新结果
        /// </summary>
        /// <param name="rid">发送资源标识</param>
        /// <param name="ssynSendResult">指示异步发送结果成功或失败[true:成功;false:失败]</param>
        internal void Update(int rid, DataTransferResult ssynSendResult)
        {
            this.RID = rid;
            this.AsynSendResult = ssynSendResult;
        }

        /// <summary>
        /// 更新结果
        /// </summary>
        /// <param name="rid">发送资源标识</param>
        /// <param name="ssynSendResult">指示异步发送结果成功或失败[true:成功;false:失败]</param>
        /// <param name="exception">异常</param>
        internal void Update(int rid, DataTransferResult ssynSendResult, Exception exception)
        {
            this.RID = rid;
            this.AsynSendResult = ssynSendResult;
            this.Exception = exception;
        }
    }


    /// <summary>
    /// 数据传输收到的数据输出事件参数
    /// </summary>
    public class DataTransferDataOutputArgs : EventArgs
    {
        /// <summary>
        /// 接收到的数据
        /// </summary>
        public BufferData Data { get; protected set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DataTransferDataOutputArgs()
        {

        }

    }


}
